﻿using HZH_Controls.Forms;
using Petroleum.DB.Model;
using Petroleum.Model;
using Petroleum.Tool;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Petroleum.UI
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            #region ==创建数据库==
            SQLiteHelper sQLiteHelper;
            //需要以管理员权限执行，确保数据库创建成功
            if (!File.Exists(ResServer.DBURL))
            {
                MessageBox.Show("DB加载失败");
            }
            else
            {
                sQLiteHelper = new SQLiteHelper(ResServer.DBURL);
            }
            #endregion
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //BasicInf basicInf = new BasicInf()
            //{
            //    WellName = "三月的井",
            //    WellType = "天然气+石油混合井",
            //    Wellhead =  new WellheadCoordinates() { Latitude = "12313", Longitude = "41431" },
            //    WellCompletionMethod = "没有方法",
            //    GL = "GL123",
            //    KB = 999,
            //    SSTVD = "213198",
            //    TrajectoryType = "TrajectoryType",
            //    LocationOfGeoStructure = "陕西省",
            //    DesignTVD = "131",
            //    NameOfGeoStructure = "NameOfGeoStructure",
            //    PurposeOfDrilling = "挣钱",
            //    TargetFormation = "咋形成",
            //    TDCriterion = "GB78136",
            //};
            //FormationPressure formation = new FormationPressure()
            //{
            //    ImgE1 = @"C:\Users\chenz\Pictures\一片树叶的撼动.jpg"
            //};

            #region MyRegion
            //全局异常捕捉
            //处理未捕获的异常，始终将异常传送到 ThreadException 处理程序。忽略应用程序配置文件。   
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            //订阅ThreadException事件，处理UI线程异常，处理方法为 Application_ThreadException，关于事件的相关知识就不在这叙述了  
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            //订阅UnhandledException事件，处理非UI线程异常 ，处理方法为 CurrentDomain_UnhandledException  
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            #endregion
            //WordUtil word = new WordUtil();
            //word.testimg(@"D:\Desktop\Word模板版.docx","ImgE1", formation.ImgE1,"img");
            Application.Run(new FormMain());
            //Application.Run(new Form2());
            //Application.Run(new Form1());
        }
        //UI线程异常
        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            //string str = "";
            //string strDateInfo = "出现应用程序未处理的异常：" + DateTime.Now.ToString() + "/r/n";
            //Exception error = e.Exception as Exception;
            //if (error != null)
            //{
            //    str = string.Format(strDateInfo + "异常类型：{0}/r/n异常消息：{1}/r/n异常信息：{2}/r/n",
            //         error.GetType().Name, error.Message, error.StackTrace);
            //}
            //else
            //{
            //    str = string.Format("应用程序线程错误:{0}", e);
            //}
            MessageBox.Show("网络异常，请稍后再试或者尝试重启", "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        //多线程异常
        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
        }
    }
}
